package com.miaomiaomiao.gegege.controller;

import java.io.OutputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.jboss.logging.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;





import com.miaomiaomiao.gegege.bo.Dog;
import com.miaomiaomiao.gegege.bo.GeAndMiao;
import com.miaomiaomiao.gegege.bo.GeContent;
import com.miaomiaomiao.gegege.service.GeService;
import com.miaomiaomiao.gegege.service.PicService;

@Controller
public class Ge {
	@Resource(name="geService")
	private GeService geService;
	
	@RequestMapping("/")
	public String gegege(Model model) {
		//测试请求分派到view
		List<Map<String,Object>> list = geService.showGe();
		model.addAttribute("list", list);
		return "index";
	}
	
	@RequestMapping("/sayGe")
	@ResponseBody
	public Object sayGe(@RequestParam("ge1") String ge1, Model model) {
		GeContent ge = new GeContent ();
		ge.setDate(new java.sql.Date(new Date().getTime()));
		if (ge1 == null || ge1.trim().isEmpty()) {
			ge.setContent("null");
		} else ge.setContent(ge1);
		geService.addGe(ge);

		List<Map<String,Object>> list = geService.showGe();
		return list;
	}
	

	@RequestMapping("/addGege/{str}")
	@ResponseBody
	public Object ge(@PathVariable String str) {
		//测试类似RESTFUL的路径
		GeContent ge = new GeContent ();
		ge.setDate(new java.sql.Date(new Date().getTime()));
		if (str == null || str.trim().isEmpty()) {
			ge.setContent("null");
		} else ge.setContent(str);
		
		geService.addGe(ge);
		
		Map<String, Object> m = new HashMap<String,Object> ();
		m.put("aaa", Arrays.asList(1,2,3,4,5));
		m.put("bbbbbb", Arrays.asList(1313,222));
		m.put("gegege", new Dog());
		m.put("content", str);

		return m;
	}
	
	@RequestMapping("/gege2")
	@ResponseBody
	public Object gege2(@ModelAttribute("gam") GeAndMiao gam) {
		//测试用BO传参数
		return gam;
	}
	
	
	private Logger log = Logger.getLogger(getClass());
	
	@Resource(name="picService")
	private PicService picService;
	
	@RequestMapping("/up")
	@ResponseBody
	public Object upload(MultipartHttpServletRequest  request, HttpServletResponse response) {
		MultipartFile  file = (MultipartFile) ( request.getFile("upfile"));
		String note = request.getParameter("note");
		log.info(note);
		
		try {
			byte[] nB = new byte[file.getBytes().length];
			System.arraycopy(file.getBytes(), 0, nB, 0, file.getBytes().length);
			long id = picService.up(nB);
			return id;
/*			String basePath = servletContext.getRealPath("fileDir");  
			System.out.println( basePath + "\n");
			File f = new File(basePath + File.separator + note);
			System.out.println(f.getPath());
			f.createNewFile();
			OutputStream os = new FileOutputStream(f);
			os.write(file.getBytes());
			os.close();*/
		} catch (Exception ex) {
			ex.printStackTrace();
			return ex;
		}
	}
	
	
	@RequestMapping("/pic/{picId}")
	public void qPic(@PathVariable String picId, HttpServletRequest request, HttpServletResponse response) {
		long nid = Long.valueOf(picId);
		byte[] b = picService.q(nid);
		response.setContentType("image/jpeg");
		OutputStream os = null;
		try {
			os = response.getOutputStream();
			os.write(b);
		} catch (Exception ex) {
			
		} finally {
			IOUtils.closeQuietly(os);
		}
	}

}






